<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>点击显示当前系统时间</title>
  <style>
    div,
    p {
      height: 30px;
      width: 300px;
      background-color: pink;
    }
  </style>
</head>
<body>
<button>显示当前系统时间</button>
<div>点击上方按钮显示当前时间</div>
<p></p>
<script>
  // 1.获取元素
  // var btn = document.querySelector('button')
  // var div = document.querySelector('div')
  document.querySelector('button').onclick = function () {
    document.querySelector('div').innerText = (getDate())
  }

  function getDate () {
    var date = new Date()
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var dates = date.getDate()
    var arr = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
    var day = arr[date.getDay()]

    var time = new Date()
    var h = time.getHours()
    h = h < 10 ? '0' + h : h
    var m = time.getMinutes()
    m = m < 10 ? '0' + m : m
    var s = time.getSeconds()
    s = s < 10 ? '0' + s : s
    return ('今天是' + year + '年' + month + '月' + dates + '日 ' + day + h + ':' + m + ':' + s)
  }

  // 我们元素可以不用添加事件
  var p = document.querySelector('p')
  p.innerText = getDate()        //刷新才会变
</script>
</body>
</html>